<?php

namespace common\observer\task\version;

use teamones\dbeventObserver\interfaces\SplObserver;
use teamones\dbeventObserver\interfaces\SplSubject;
use common\model\VersionModel;
use support\SingletonTrait;
use Throwable;

/**
 * 内部任务版本发布 记录发布时间
 */
class InsideVersionPassRecordEndTime implements SplObserver
{
    use SingletonTrait;

    /**
     * @inheritDoc
     */
    function update(SplSubject $subject): void
    {
        $updateData = $subject->getSplUpdateData();
        if (!isset($updateData['is_pass']) || $updateData['is_pass'] !== 'yes') {
            return;
        }
        try {
            model(VersionModel::class)->modifyItem(['id' => $subject->getSplPrimaryId(), 'end_time' => time()]);
        } catch (Throwable $e) {
            trace(format_exception_info($e), 'ERR');
        }
    }
}
